package com.yuexun.dhdoctor.module.personalPlan.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseEntity;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.personalPlan.model.impl.PlanModel;
import com.yuexun.dhdoctor.module.personalPlan.presenter.impl.ScheduleAddPresenter;
import com.yuexun.dhdoctor.module.personalPlan.ui.view.IScheduleAddView;
import com.yuexun.dhdoctor.widget.pickerUtil.CustomDatePicker;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/4/20 0020.
 */

public class Act_Schedule_Add extends BaseKJActivity implements IScheduleAddView {

    @Bind(R.id.schedule_date)
    TextView scheduleDate;
    @Bind(R.id.schedule_time)
    TextView scheduleTime;
    @Bind(R.id.remind_time_view)
    LinearLayout remindTimeView;
    @Bind(R.id.schedule_edt)
    EditText scheduleEdt;
    @Bind(R.id.title_tv)
    TextView titleTv;
    @Bind(R.id.rb_schedule_unremind)
    RadioButton rbScheduleUnremind;
    @Bind(R.id.rb_schedule_remind)
    RadioButton rbScheduleRemind;
    @Bind(R.id.schedule_week)
    TextView scheduleWeek;

    String date;
    String week;
    String type;
    String content;
    String remindTime;
    String selectdate;
    boolean isRemind;
    ScheduleAddPresenter presenter;

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    @Override
    public void setRootView() {
        super.setRootView();
        setContentView(R.layout.act_schedule_add);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        type = getIntent().getExtras().getString("type");
        date = getIntent().getExtras().getString("datetime");
        week = getIntent().getExtras().getString("week");
        if (type.equals("edit")) {
            titleTv.setText("编辑日程");
            content = getIntent().getExtras().getString("content");
            remindTime = getIntent().getExtras().getString("remindTime");
        }
        selectdate = date;
        scheduleDate.setText(date);
        try {
            Date da = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            initWeek(da);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        scheduleTime.setText(new SimpleDateFormat("HH:mm").format(new Date()));

        if (content != null && !content.isEmpty()) {
            scheduleEdt.setText(content);
        }
        if (remindTime != null && !remindTime.isEmpty()) {
            remindTimeView.setVisibility(View.VISIBLE);
            rbScheduleRemind.setChecked(true);
            isRemind = true;
            try {
                Date time = new SimpleDateFormat("yyyy-MM-dd HH:mm").
                        parse(remindTime);
                scheduleTime.setText(new SimpleDateFormat("HH:mm").format(time));
                selectdate = new SimpleDateFormat("yyyy-MM-dd").format(time);
                scheduleDate.setText(selectdate);
                initWeek(time);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        presenter = new ScheduleAddPresenter(new PlanModel(), this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back, R.id.save, R.id.rb_schedule_unremind, R.id.rb_schedule_remind, R.id.schedule_time,
            R.id.schedule_date})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save:
                if (!scheduleEdt.getText().toString().trim().isEmpty()) {
                    try {
                        String timr = selectdate + " " + scheduleTime.getText().toString();
                        Date datetime = format.parse(timr);
                        Long sdate = dateFormat.parse(selectdate).getTime();

                        Map<String, String> params = new HashMap<>();
                        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                        params.put("content", scheduleEdt.getText().toString().trim());
                        if (isRemind) {
                            if (datetime.getTime() > new Date().getTime() && sdate <= dateFormat.parse(date).getTime()) {
                                params.put("remindFlag", "1");
                                params.put("remindTime", selectdate + " " + scheduleTime.getText().toString());
                            } else {
                                ViewInject.toast("提醒时间设置错误");
                                break;
                            }
                        } else {
                            params.put("remindFlag", "0");
                        }
                        if (type.equals("edit")) {
                            params.put("scheduleId", getIntent().getExtras().getString("scheduleId"));
                            presenter.updateSchedule(params);
                        } else {
                            params.put("date", date);
                            presenter.createSchedule(params);
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                } else {
                    ViewInject.toast("请输入日程内容");
                }
                break;
            case R.id.rb_schedule_unremind:
                remindTimeView.setVisibility(View.GONE);
                isRemind = false;
                break;
            case R.id.rb_schedule_remind:
                remindTimeView.setVisibility(View.VISIBLE);
                isRemind = true;
                break;
            case R.id.schedule_time:
                initTimePicker();
                break;
            case R.id.schedule_date:
                initDatePicker();
                break;
            default:
                break;
        }
    }

    private void initDatePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        Date date1 = null;
        try {
            date1 = sdf.parse(date + " 00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String now = sdf.format(date1);

        CustomDatePicker customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                selectdate = time;
                scheduleDate.setText(time);
                try {
                    Date da = new SimpleDateFormat("yyyy-MM-dd").parse(time);
                    initWeek(da);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecificTime(false); // 显示时和分
        customDatePicker1.setIsLoop(true); // 不允许循环滚动
        customDatePicker1.show(now);
    }

    private void initTimePicker() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        String now = sdf.format(new Date());

        CustomDatePicker customDatePicker1 = new CustomDatePicker(this, new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) { // 回调接口，获得选中的时间
                scheduleTime.setText(time);
            }
        }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
        customDatePicker1.showSpecificTime(true); // 显示时和分
        customDatePicker1.showSpecificDate(false);// 不显示日期
        customDatePicker1.setIsLoop(true); // 不允许循环滚动
        customDatePicker1.show(now);
    }

    private void initWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == 6) {
            week = "周五";
            scheduleWeek.setText("周五");
        } else if (day == 7) {
            week = "周六";
            scheduleWeek.setText("周六");
        } else if (day == 1) {
            week = "周日";
            scheduleWeek.setText("周日");
        } else if (day == 2) {
            week = "周一";
            scheduleWeek.setText("周一");
        } else if (day == 3) {
            week = "周二";
            scheduleWeek.setText("周二");
        } else if (day == 4) {
            week = "周三";
            scheduleWeek.setText("周三");
        } else if (day == 5) {
            week = "周四";
            scheduleWeek.setText("周四");
        }
    }

    @Override
    public void createSchedule(BaseEntity baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            if (type.equals("edit")) {

            } else {
                ViewInject.toast("创建成功");
            }
            finish();
        } else {
            ViewInject.toast(baseEntity.getMessage());
        }
    }
}
